package controller;

import java.awt.Dimension;

import javax.swing.JOptionPane;

import domain.DomainException;
import domain.Speler;
import view.BoardView;
import view.UIException;

import model.Game;

public class TicTacToeController implements Controller{
	private Game model;
	private Speler speler;
	private BoardView view;
	
	public TicTacToeController(Game ticTacToeGame, Speler player){
		this.model = ticTacToeGame;
		this.speler = player;
		
		model.voegToeAanTitel(speler.getSpelerNaam());
		
		try {			
			view = new BoardView(this);
			view.setVisible(true);
		} catch (UIException e) {
			JOptionPane.showInputDialog(e.getMessage());
		}
		ticTacToeGame.registerObserver(view);
	}

	@Override
	public Dimension getBoardSize() {
		Dimension boardSize = new Dimension(getNumberOfColumns() * 80, getNumberOfRows() * 80);
		return boardSize;
	}
	
	@Override
	public int getNumberOfRows() {
		return model.getBoard().getHeight();
	}

	@Override
	public int getNumberOfColumns() {
		return model.getBoard().getWidth();
	}


	@Override
	public void enable(boolean enabled) {
		view.setEnabled(enabled);
	}

	@Override
	public void play(int rowClicked, int columnClicked) {
		try {
			model.play(rowClicked, columnClicked);
		} catch (DomainException ex) {
			JOptionPane.showMessageDialog(null, ex.getMessage());
		}
	}

	@Override
	public Game getGame() {
		return model;
	}
}
